package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.vo.House;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.net.SharedObject;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.base.MediatorMap;
	import org.robotlegs.mvcs.Mediator;
	
	public class SaveDesignVerifyTitleWinMediator extends Mediator
	{
		[Inject]
		public var view:SaveDesignVerifyTitleWindow;
		
		[Inject]
		public var roomModel:ExchangeDataModel;

		[Inject]
		public var userModel:UserInforModel;
		private var nc:NetConnection;
		
		public function SaveDesignVerifyTitleWinMediator()
		{
			super();
		}
		
		public override function onRegister():void
		{
			view.closeBtn.addEventListener(MouseEvent.CLICK,closeBtnClickHandler);
			view.saveBtn.addEventListener(MouseEvent.CLICK,saveBtnClickHandler);
			view.unsaveBtn.addEventListener(MouseEvent.CLICK,unsaveBtnClickHandler);
			view.cancelBtn.addEventListener(MouseEvent.CLICK,cancelBtnClickHandler);
		}
		public function cancelBtnClickHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
		}
		
		public function unsaveBtnClickHandler(event:MouseEvent):void
		{				
			PopUpAdmin.popupCenterWindow("entryTitle", view.parent, mediatorMap);
			PopUpManager.removePopUp(view);
		}
			
		public function closeBtnClickHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
		}
		
		public function saveBtnClickHandler(event:MouseEvent):void
		{
			var sharedObject:SharedObject = SharedObject.getLocal("User");
			var roomCount:int = roomModel.gridContainer.numChildren;
			if (!userModel.isLogined && !roomModel.isOpened)
			{
				var enterWindow:LoginTitleWindow = new LoginTitleWindow();
				enterWindow.name = "saveDesign";
				PopUpManager.addPopUp(enterWindow, view.parent, true);
				PopUpManager.centerPopUp(enterWindow);
				mediatorMap.createMediator(enterWindow);
			}
			else if(userModel.isLogined && !roomModel.isOpened)
			{ 
				switch(sharedObject.data.userTypeID)
				{
					case 0:
						PopUpAdmin.popupCenterWindow("saveDesigns", view.parent, mediatorMap);
						break;
					case 1:
						PopUpAdmin.popupCenterWindow("saveHouse", view.parent, mediatorMap);
						break;
				}
			}
			else if(userModel.isLogined && roomModel.isOpened)
			{
				Alert.show("保存修改");
			}
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
		
		private function getImg():BitmapData
		{
			var minX:Number = 10000;
			var minY:Number = 10000;
			var maxX:Number = -1;
			var maxY:Number = -1;
			var target:UIComponent = roomModel.gridContainer;
			var boundingBox:Rectangle;
			for (var i:int = 0; i < roomModel.gridContainer.numChildren; i++)
			{
				if ((roomModel.gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(roomModel.gridContainer.getChildAt(i) is IndependentFloor))
				{
					var host:AbstractDrawingRoom = roomModel.gridContainer.getChildAt(i) as AbstractDrawingRoom;
					boundingBox = host.getRect(host.parent);
					if (boundingBox.x <= minX)
						minX = boundingBox.x;
					if (boundingBox.y <= minY)
						minY = boundingBox.y;
					if (boundingBox.x + boundingBox.width >= maxX)
						maxX = boundingBox.x + boundingBox.width;
					if (boundingBox.y + boundingBox.height >= maxY)
						maxY = boundingBox.y + boundingBox.height;
				}					
			}
			var matrix:Matrix = new Matrix();
			matrix.translate(-minX + 32, -minY + 32);
			var bitmapData:BitmapData = new BitmapData(maxX - minX + 64, maxY - minY + 64);
			bitmapData.draw(target,matrix); 
			return bitmapData;
		}
		
		private function successResult(result:String):void 
		{
			//关闭连接
			nc.close();
		}
		private function errStatus(result:String):void 
		{
			nc.close();
		}
	}
}